package com.sangmo.fts.user.model;

import com.sangmo.boot.framework.common.session.UserInterface;
import com.sangmo.boot.framework.data.entity.general.SoftDeleteEntity;
import com.sangmo.boot.framework.data.json.meta.ExtView;
import com.sangmo.boot.framework.data.json.meta.Many2Many;
import com.sangmo.boot.framework.data.json.meta.One;
import com.sangmo.boot.framework.plugin.dataview.def.meta.DataField;
import com.sangmo.boot.framework.plugin.dataview.def.meta.DataView;
import com.sangmo.fts.clazz.model.Clazz;
import com.sangmo.fts.clazz.model.UserClazz;
import com.sangmo.fts.common.IConstants;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLDeleteAll;
import org.hibernate.annotations.Where;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import java.beans.Transient;

@Entity
@Table(name = "user")
@SQLDelete(
        sql = "UPDATE #{entityName} SET deleted = CURRENT_TIMESTAMP WHERE id = ?"
)
@SQLDeleteAll(
        sql = "UPDATE #{entityName} SET deleted = CURRENT_TIMESTAMP WHERE id = ?"
)
@Where(clause = "deleted = 0")
@ExtView
@DataView
public class User extends SoftDeleteEntity implements UserInterface {

    @Many2Many(src="id", value = "clazzs",size = 100,target = Clazz.class, ref="userId", targetRef="clazzId", mediator= UserClazz.class)

    @DataField(value = IConstants.DATA_TYPE.STRING_50, required = true)
    private String exRoleId;

    @DataField(value = IConstants.DATA_TYPE.LONG, required = true)
    @One(value = "role", target = Role.class)
    private Long roleId;

    @DataField(value = IConstants.DATA_TYPE.STRING_50, required = true)
    private String exUserId;

    private String schoolName;

    private String exSchoolId;

    @DataField(value = IConstants.DATA_TYPE.STRING_50, required = true, index = true)
    private String loginName;

    @DataField(value = IConstants.DATA_TYPE.STRING_50, required = true)
    private String password;

    private String number;

    @DataField(value = IConstants.DATA_TYPE.STRING_50, required = true)
    private String name;

    private String email;

    private String mobile;

    private String state = States.ENABLED;

    private String description;

    public Long getRoleId() {
        return roleId;
    }

    public void setRoleId(Long roleId) {
        this.roleId = roleId;
    }

    public String getExRoleId() {
        return exRoleId;
    }

    public void setExRoleId(String exRoleId) {
        this.exRoleId = exRoleId;
    }

    public String getExUserId() {
        return exUserId;
    }

    public void setExUserId(String exUserId) {
        this.exUserId = exUserId;
    }

    public String getExSchoolId() {
        return exSchoolId;
    }

    public void setExSchoolId(String exSchoolId) {
        this.exSchoolId = exSchoolId;
    }

    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Transient
    public boolean isEnabled() {
        return States.ENABLED.equals(state);
    }

    public interface States extends IConstants.ENABLE_STATE {
    }
}
